
#include "../common.h"

void print_file_mode(mode_t mode)
{
	char buf[28];

	if (S_ISREG(mode)) sprintf(buf, "regular");
	else if (S_ISDIR(mode))  sprintf(buf, "regular");
	else if (S_ISCHR(mode))  sprintf(buf, "character");
	else if (S_ISBLK(mode))  sprintf(buf, "block");
	else if (S_ISFIFO(mode)) sprintf(buf, "fifo");
	else if (S_ISLNK(mode))  sprintf(buf, "link");
	else if (S_ISSOCK(mode)) sprintf(buf, "sock");
	else sprintf(buf, "unknown mode");

	printf("%s\n", buf);

	return;
}

int main(int argc, char** argv)
{
	int i;
	struct stat buf;

	for (i = 1; i < argc; i++)
	{
		printf("%s--\n", argv[i]);
		if (stat(argv[i], &buf) == -1)	
		{
			printf(" stat failed\n");
			continue;
		}
		print_file_mode(buf.st_mode);

		if (lstat(argv[i], &buf) == -1)	
		{
			printf(" stat failed\n");
			continue;
		}
		print_file_mode(buf.st_mode);

		if (fstat(open(argv[i], O_RDWR), &buf) == -1)	
		{
			printf(" stat failed\n");
			continue;
		}
		print_file_mode(buf.st_mode);

		printf("---end\n");
	}

	return 0;
}
